import java.util.*;

public class SCAN extends DiskScheduler
{
    public int [][] surface;
    Request target;
    boolean moveUp = false;
    int currentTrack;
    int currentSector;
    
    public SCAN(HardDisk hd)
    {
        super(hd);
        this.surface = hardDisk.platter.surface;

        
    }
    
    public void addRequest(Request r)
    {
        
    }
    
    public void removeRequest(Request r)
    {
        
    }
    
    public void tick()
    {
        currentTrack = hardDisk.headTrack;
        currentSector = hardDisk.headSector;
        
	    target = null;
        try
        {
            if(target==null)
            for(int i=currentSector;i<hardDisk.sectorNumber;i++)
            {
                if(surface[currentTrack][i]!=0)
                {
                    target = new Request(currentTrack, i);
                    break;
                }
            }
            
            if(target==null)
            for(int i=0;i<currentSector;i++)
            {
                if(surface[currentTrack][i]!=0)
                {
                    target = new Request(currentTrack, i);
                    break;
                }
            }
        }
        catch(NoSuchElementException e)
        {
            target = null;    
        }
        
        if(target==null)
        {
            if(moveUp)
            {
                hardDisk.targetTrack--;
                if(hardDisk.targetTrack<0)
                {
                    moveUp=false;
                    hardDisk.targetTrack=0;
                }
            }
            else
            {
                hardDisk.targetTrack++;
                if(hardDisk.targetTrack>=hardDisk.trackNumber)
                {
                    moveUp=true;
                    hardDisk.targetTrack=hardDisk.trackNumber-1;
                }
            }
        }
        else
        {
            hardDisk.targetTrack = target.track;    
            hardDisk.targetSector = target.sector;    
        }
    }
        
}